﻿using ProjectCloth.Entity;
using ProjectCloth.FrmUI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class GeneralExpListFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        public GeneralExpListFrm()
        {
            InitializeComponent();
            var acountList = db.CashAccounts.ToList();
            outmoneycbb.DataSource = acountList;
            outmoneycbb.ValueMember = "Id";
            outmoneycbb.DisplayMember = "TradeTypeName";
            outmoneycbb.SelectedValue = 0;
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = (from ge in db.GeneralExpenseses.ToList()
                                        where ge.CreateTime > startDatePiker.Value && ge.CreateTime < endDatePicker.Value
                                        && (string.IsNullOrEmpty(summaryTxt.Text) || ge.Summary.Contains(summaryTxt.Text))
                                        && (string.IsNullOrEmpty(RemarkTxt.Text) || ge.Remark.Contains(RemarkTxt.Text))
                                        && (outmoneycbb.SelectedValue == null || (int)outmoneycbb.SelectedValue <= 0 || ge.PayAccountId == (int)outmoneycbb.SelectedValue)
                                        && (outmoneycbb.SelectedValue == null || (int)outmoneycbb.SelectedValue <= 0 || ge.PayAccountId == (int)outmoneycbb.SelectedValue)
                                        select ge).ToList();
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            Helper.ExportDgv(dataGridView1, "一般费用列表");
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && dataGridView1.CurrentRow != null)
            {
                var exId = dataGridView1.CurrentRow.Cells["Id"].Value;
                if (exId != null && (int)exId > 0)
                {
                    var itemList = from gei in db.GeneralExpensesItems.Where(o => o.GeneralExpensesId == (int)exId)//.ToList()
                                   join s in db.Subjects on gei.SubjectId equals s.Id
                                   select new
                                   {
                                       subjectName = s.Name,
                                       Amount = gei.Cost,
                                       Remark = gei.Remark
                                   };
                    dataGridView2.AutoGenerateColumns = false;
                    dataGridView2.DataSource = itemList.ToList();
                }
            }
        }

        private void remakebtn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(remarkText.Text))
            {
                var ge = db.GeneralExpensesItems.Where(o => o.Remark.Contains(remarkText.Text)).ToList();
                dataGridView2.AutoGenerateColumns = false;
                dataGridView2.DataSource = ge;
            }
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && dataGridView2.CurrentRow != null)
            {
                var exId = dataGridView2.CurrentRow.Cells["GeneralExpensesId"].Value;
                if (exId != null && (int)exId > 0)
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource =  db.GeneralExpenseses.Where(o=>o.Id.Equals((int)exId)).ToList()
                                                ;
                }
            }
        }
    }
}